<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('农事记录')"/>
    <style>
        .fixed-table-toolbar .bs-bars {
            width: 100%;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">

        <div class="col-sm-12 search-collapse">
            <div class="btn-group " style="margin: 0px auto;display: table;" role="group">
                <button type="button" class="top-link btn btn-sm btn-white">当天农事</button>
                <button type="button" class="top-link btn-sm btn-primary">在执行</button>
                <button type="button" class="top-link btn-sm btn-white">待执行</button>
            </div>

        </div>

        <div class="btn-group-sm " id="toolbar1" role="group">
            <h3 class="inline">工作内容：</h3>
        </div>
        <div class="col-sm-12 select-table table-striped ">
            <table id="bootstrap-table-1" data-mobile-responsive="true"></table>
        </div>

        <!--计划详情-->
        <div class="col-sm-12 search-collapse">
            <h3>计划详情：</h3>
            <div class="thumbnail col-sm-3" style="width: 140px; height: 140px;">
                <img class="standard-img" src="/img/profile.jpg">
            </div>
            <div class="col-sm-9 detail-box">
                <div class="ibox-title">
                    <h4 class="stageName">生产阶段：</h4>
                </div>
                <div class="ibox-title">
                    <h4 class="inline">计划编号：</h4><span class="kv-v planCode">--</span>
                    <h4 class="inline">作物名称：</h4><span class="kv-v cropNameCn">--</span>
                    <h4 class="inline">作物品种：</h4><span class="kv-v cropVarietiesCn">--</span>
                    <h4 class="inline">计划时间：</h4><span class="kv-v planTime">--</span>
                </div>
                <div class="btn-group ibox-title" role="group">
                    <button type="button" class="work-logs middle-link btn-sm btn-primary">农事记录</button>
                    <button type="button" class="zhibao-logs middle-link btn-sm btn-primary">植保请求记录</button>
                    <button type="button" class="zhibao-add middle-link btn-sm btn-success">植保请求</button>
                    <button type="button" class="nongzi-add middle-link btn-sm btn-success">新增农资</button>
                    <button type="button" class="work-add middle-link btn-sm btn-success">新增农事</button>

                    <!--<button th:hidden="${type==1}" type="button" class="edit-plan middle-link btn-sm btn-success">修改大棚-->
                    </button>
                </div>
            </div>
        </div>

    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:ncWorkLog:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:ncWorkLog:remove')}]];
    var prefix = ctx + "system/ncWorkLog";
    var planType = [[${type}]];
    $(function () {
        initTodayWorks();

        $('.top-link').click(function () {
            var index = $(this).index();
            switch (index) {
                case 0:
                    window.location.href = ctx + 'system/ncWorkLog?type=' + planType;
                    return;
                case 1:
                    window.location.reload();
                    return;
                case 2:
                    window.location.href = ctx + 'system/ncWorkLog/ncWorkLogPreWorking?type=' + planType;
                    return;

            }
        })
    });

    var fillPlanDetail = function (row, $element) {
        //阶段
        var str = '阶段：';
        if (row.stages) {
            $(row.stages).each(function (i, e) {
                /*1:完成 2：执行中 3：未进入*/
                var color = '';
                if (i >= topColor.length) {
                    color = topColor[i - topColor.length];
                } else {
                    color = topColor[i];
                }
                if (e.stageStatus == 1) {
                    str += '<span class="finished" style="background-color: '+color+'">' + e.stageName + '</span>'
                } else if (e.stageStatus == 2) {
                    str += '<span class="working" style="background-color: '+color+'">' + e.stageName + '<i class="fa fa-circle text-danger" style="margin-left: 2px"></i></span>'
                } else if (e.stageStatus == 3) {
                    str += '<span class="pre-working" style="background-color: '+color+'">' + e.stageName + '</span>'
                }

            });
        }
        if (!row.plan) {
            row.plan = {};
        }

        $('.stageName').html(str);
        $('.planCode').html(row.plan.planCode);
        $('.cropNameCn').html(row.plan.cropNameCn);
        $('.cropVarietiesCn').html(row.plan.cropVarietiesCn);
        $('.planTime').html(dateFtt(row.plan.planTime));
        if (row.standard && row.standard.pic) {
            $('.standard-img').attr('src', row.standard.pic)
        } else {
            $('.standard-img').attr('src', '/img/profile.jpg')
        }


        $('.detail-box').attr('planId', row.plan.planId)
        $('.detail-box').attr('stageId', row.stageMirrorId)
    }

    /*初始化当天工作*/
    function initTodayWorks() {
        var prefix = ctx + "system/ncWork";

        var options = {
            url: prefix + "/listOnExe",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "标准农事",
            id: 'bootstrap-table-1',
            pageSize: 8,
            sidePagination: "client",
            toolbar: 'toolbar1',
            showSearch: false,
            showRefresh: false,
            showColumns: false,
            showToggle: false,
            showExport: false,
            pagination:true,
            queryParams: {params: {planType: planType}},
            onClickRow: fillPlanDetail,
            onLoadSuccess: function (data) {
                if (data.length > 0) {
                    fillPlanDetail(data[0]);
                }
            },
            columns: [{
                title: '序号',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }, {
                field: 'stageMirrorId',
                title: '阶段镜像id',
                visible: false
            }, {
                field: 'plan.planCode',
                title: '计划编号',
                visible: true

            }, {
                field: 'stageName',
                title: '阶段名称',
                visible: true
            }, {
                title: '详细信息',
                visible: true,
                formatter: function (value, row, index) {
                    var str = '';
                    if (!row.plan) {
                        row.plan = {};
                    }
                    // str += row.plan.planCode + " - ";
                    str += row.plan.cropNameCn + " - ";
                    str += row.plan.cropVarietiesCn + " - ";
                    // str += dateFtt(row.plan.planTime) + " - ";
                    str += !row.workName ? '' : row.workName + " - ";
                    str +=  row.plan.sumArea?row.plan.sumArea:'**' +'亩'+" - ";
                    str += !row.plan.peng ? '**' : row.plan.peng ;

                    return str;
                }
            },
                {
                    field: 'workDate',
                    title: '开始时间',
                    formatter: function (value, row, index) {
                        return dateFtt(value)
                    }
                },
                {
                    field: 'workEndDate',
                    title: '结束时间',
                    formatter: function (value, row, index) {
                        return dateFtt(value)
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];

                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="preFinish(\'' + row.stageMirrorId + '\')"><i class="fa fa-edit"></i>提前结束</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="daPeng(\'' + row.planId + '\')"><i class="fa fa-remove"></i>分配大棚</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

        //按钮事件
        $('.work-logs').click(function () {
            var planId = $('.detail-box').attr('planId');
            if (valid(planId)) {
                toAllLogs(planId);
            }
        });
        $('.zhibao-logs').click(function () {
            var planId = $('.detail-box').attr('planId');
            if (valid(planId)) {
                var url = ctx + 'system/ncZhibao/ncZhibaoLogs?planId=' + planId;
                $.modal.openTab('请求记录', url);
            }
        });

        $('.zhibao-add').click(function () {
            var planId = $('.detail-box').attr('planId');
            var stageId = $('.detail-box').attr('stageId');
            if (valid(planId)) {
                var url = ctx + 'system/ncZhibao/addRequest?planId=' + planId + "&stageId=" + stageId;
                $.modal.open('植保请求', url);
            }
        });
        $('.nongzi-add').click(function () {
            var planId = $('.detail-box').attr('planId');
            var stageId = $('.detail-box').attr('stageId');
            if (valid(planId)) {
                var url = ctx + 'system/ncWorkLog/addNongZi?planId=' + planId + "&stageId=" + stageId;
                $.modal.open('新增农资', url);
            }
        });
        $('.work-add').click(function () {
            var planId = $('.detail-box').attr('planId');
            var stageId = $('.detail-box').attr('stageId');
            if (valid(planId)) {
                var url = ctx + 'system/ncWorkLog/addNongShi?planId=' + planId + "&stageId=" + stageId;
                $.modal.open('新增农事', url);
            }
        });
        $('.auto-ctrl').click(function () {
            var url = baseWlwUrl + '/product.html';
            $.modal.openTab('智能控制', url);
        });
        $('.edit-plan').click(function () {
            var planId = $('.detail-box').attr('planId');
            var prefix = ctx + "system/ncWork";
            var url = prefix + '/editPlan?planId=' + planId;
            if (valid(planId)) {
                $.modal.open('编辑计划', url);
            }
        });


    }

    function valid(planId) {
        if (!planId) {
            $.modal.msgWarning("未发现数据!")
            return false;
        } else {
            return true;
        }
    }

    /*提前结束*/
    function preFinish(stageMirrorId) {
        var prefix = ctx + "system/ncWork";
        var url = prefix + '/preFinish';
        $.modal.confirm("确认提前完成该阶段吗？", function () {
            myPost(url, {stageMirrorId: stageMirrorId},
                function (data) {

                    if (data.code == 0) {
                        layer.msg("确认成功!")
                    } else {
                        layer.msg(data.msg);
                    }
                    reload()
                }, function (e) {
                    layer.msg("确认失败!");
                    reload()
                })

        })
    }

    /*大棚信息*/
    function daPeng(planId) {
        var prefix = ctx + "system/ncWork";
        var url = prefix + '/daPeng/' + planId;
        $.modal.open('分配大棚', url);
    }

    /*所有农事记录*/
    function toAllLogs(planId) {
        var url = prefix + "/allWorkLogs"
        if (planId) {
            url = prefix + "/allWorkLogs?planId=" + planId
        }

        $.modal.openTab('农事记录', url);
    }

    function reload() {
        $.table.refresh();
    }

    if(planType == 1){
        setTitle("育苗管理-在执行",false)
    }else {
        setTitle("温室管理-在执行",false)
    }
</script>
</body>
</html>